<?php
namespace Api\Controller;
use Think\Controller;
use Api\Event\CacheEvent;

/** 
 * Top排行接口
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

class TopController extends PublicController 
{

	/** 
	 * [top10]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function top10($type = 'daily')
	{
		$tops = event('Video')->getTop10($type);
		$vids = array_keys($tops);
		$lists = $this->lists_format($vids);
		$res = [];
		foreach ($lists as &$value) {
			$value['statis']['play'] = $tops[$value['id']];
			if ($value['type'] == 0){
				$res[] = $value;
 			}
		}
		$this->list_success($res);
	}

	/** 
	 * [推荐用户]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function user()
	{
		// $key  = 'StarUsers';
		$user_uid = $this->getUid(I('appToken'));
		$subtime = C('USER_TOP_SUB_TIME', NULL, '08:00:00');
		$user_ids = [];
		$Cackekey  = 'IMBA.Daily.TOPUSER';
		$redis = CacheEvent::instance(3);

		$res = $redis->get($Cackekey);
		if ($res){
			$user_ids = unserialize($res);
		}

		if (!$user_ids || date('H:i:s') < $subtime){
			$map = [
				'date'   => date('Ymd'),
				'status' => 1 
			];
			$user_ids = service('UserDailyTop')->where($map)->find()['uids'];

			// 如果没有 则取最新的一天的数据
			if (!$user_ids){
				$map = ['status' => 1, 'uids' => ['$ne' => null] ];
				$user_ids = service('UserDailyTop')->where($map)->find()['uids'];
			}
			// 如果还没有 就取昨天的源数据
			if (!$user_ids){
				$user_ids = $this->getAutoTopUser();
			}
			$user_ids = array_filter($user_ids);
			$key = array_search(57, $user_ids);  // 删除管理员账号
			if ($key !== false) unset($user_ids[$key]);
			$key = array_search(1, $user_ids);  // 删除管理员账号
			if ($key !== false) unset($user_ids[$key]);

			$redis->setex($Cackekey, 24*3600, serialize($user_ids));
		}
 		foreach ($user_ids as $uid) {
			$user = event('User')->detail($uid);
			if ($user)
			{
				$user_uid && $user['is_follow'] = intval(event('Follow')->is_follow($user_uid, $uid ) );
				$data[] = $user;
			}
		}
		// object(user)->api
		// $users = field_filter($data, ['id', 'avatar', 'nickname'], true, ['avatar']);
		$this->list_success($data);
	}

	protected function getAutoTopUser()
	{
		$user_ids = [];
		// $day = date('Ymd', strtotime('-1 day'));
		// #todo 计算昨日的每日之星用户
		// // video
		// // 播放次数
		// $user_ids = [];
		// $service = service('StatisDay');
		// $eventVideo  = event('Video');
		// $user_ids[] = $service->getTopVideoUser($day, 'play', $user_ids);
		// // print_r($user_ids);
		// // 评论
		// $user_ids[] = $service->getTopVideoUser($day, 'comments', $user_ids);
		// // print_r($user_ids);
		// // 点赞
		// $user_ids[] = $service->getTopVideoUser($day, 'zan', $user_ids);
		// // print_r($user_ids);
		// // 收藏
		// $user_ids[] = $service->getTopVideoUser($day, 'favour', $user_ids);
		// // user
		// // 评论
		// $user_ids[] = $service->getTopUser($day, 'comments', $user_ids);
		// // 点赞
		// $user_ids[] = $service->getTopUser($day, 'zan', $user_ids);
		$Redis = CacheEvent::instance(2);
		$user_ids = $Redis->zrevrange("IMBA.STATIS.USER.DAILYTOP.".date('Ymd', strtotime('-1 day')), 0, 10);
		return $user_ids;
	}

	/** 
	 * [每日精选]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function dailyRecomment($page = 1, $per = 10)
	{
		$res = event('Video')->getDailyRecommentVids($page, $per);
		foreach ($res as &$value) {
			$video = event('Video')->detail($value['video_id']);
			if ($video)
			{
				$value['video'] = $video;
				$dailys[] = $value;
			}
		}
		$dailys = field_filter($dailys, ['date', 'reason', 'video'], true);
		$group = [];
		foreach ($dailys as $key => $value) {
			if (!array_key_exists($value['date'], $group))
			{
				$group[$value['date']] = ['date' => $value['date']];
			}
			$group[$value['date']]['items'][] = $value;
		}
		$group = array_values($group);
		return $this->list_success($group);
	}

}